<template>
  <div class="login">
    <van-nav-bar
      title="会员登录"
      left-arrow
      @click-left="$router.go(-1)"
    ></van-nav-bar>

    <div class="container">
      <div class="title">
        <h3>手机号登录</h3>
        <p>未注册的手机号登录后将自动注册</p>
      </div>

      <div class="form">
        <div class="form-item">
          <input
            type="text"
            maxlength="11"
            placeholder="请输入手机号码"
            class="inp"
            v-model="mobile"
          />
        </div>
        <div class="form-item">
          <input
            type="text"
            maxlength="4"
            placeholder="请输入图形验证码"
            v-model="picCode"
            class="inp"
          />
          <img v-if="picUrl" :src="picUrl" @click="getPicCode" />
        </div>
        <div class="form-item">
          <input
            type="text"
            placeholder="请输入短信验证码"
            v-model="megCode"
            class="inp"
          />
          <button @click="getCode" :disabled="!(second === totalSecond)">
            {{
              second === totalSecond ? '获取验证码' : second + '秒后获取短信'
            }}
          </button>
        </div>
      </div>
      <div class="login-btn" @click="login">登录</div>
    </div>
  </div>
</template>
<script>
import { codeLogin, getPicCode, getCode } from '@/api/login'
import { showToast } from 'vant'
import 'vant/es/toast/style'

export default {
  name: 'LoginIndex',

  data() {
    return {
      picCode: '',
      picKey: '',
      picUrl: '',
      second: 60, // 当前秒数
      totalSecond: 60, // 总秒数
      timer: null, // 定时器
      mobile: '',
      megCode: ''
    }
  },

  async created() {
    this.getPicCode()
  },

  methods: {
    async getPicCode() {
      const {
        data: { base64, key }
      } = await getPicCode()
      this.picUrl = base64
      this.picKey = key
      showToast({
        message: '获取验证码成功'
      })
    },

    // 获取短信验证码
    async getCode() {
      if (!this.vaildFn()) {
        return
      }
      // 这里只写成功的例子
      await getCode(this.picCode, this.picKey, this.mobile)
      showToast('短信发送成功')

      if (!this.timer && this.second === this.totalSecond) {
        this.timer = setInterval(() => {
          this.second--
          if (this.second <= 0) {
            clearInterval(this.timer)
            this.second = this.totalSecond
            this.timer = null
          }
        }, 1000)
      }
    },

    // 校验手机号 和 图新验证码 是否合法
    vaildFn() {
      if (!/^1[3-9]\d{9}$/.test(this.mobile)) {
        showToast('请输入正确的手机号')
        return false
      }
      if (!/^\w{4}$/.test(this.picCode)) {
        showToast('请输入正确的验证码')
        return false
      }
      return true
    },

    // 登录
    async login() {
      if (!this.vaildFn()) {
        return
      }

      if (!/^\d{6}$/.test(this.megCode)) {
        showToast('请输入正确的手机验证码')
        return
      }

      const res = await codeLogin(this.megCode, this.mobile)
      showToast('登陆成功')
      this.$store.commit('user/setUserInfo', res.data)
      const url = this.$route.query.backUrl || '/home'
      this.$router.replace(url)
    }
  },

  // 运用计时器要记得销毁，不然会后台运行
  unmounted() {
    clearInterval(this.timer)
  }
}
</script>
<style scoped>
.container {
  padding: 49px 29px;
  .login-btn {
    height: 42px;
    width: 100%;
    margin-top: 39px;
    color: #fff;
    background: linear-gradient(90deg, #ecb53c, #ff9211);
    box-shadow: rgba(0, 0, 0, 0.25) 0px 14px 28px,
      rgba(0, 0, 0, 0.22) 0px 10px 10px;
    border-radius: 39px;
    letter-spacing: 20px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}

.title {
  margin-bottom: 20px;
  .h3 {
    font-size: 26px;
    font-weight: normal;
  }
  p {
    line-height: 40px;
    font-size: 14px;
    color: #b8b8b8;
  }
}

.form {
  .form-item {
    border-bottom: 1px solid #f3f1f2;
    padding: 8px;
    margin-bottom: 14px;
    display: flex;
    align-items: center;
    .inp {
      display: block;
      border: none;
      /* outline: none; 与border类似又有不同 */
      height: 32px;
      font-size: 14px;
      flex: 1;
    }
    img {
      width: 94px;
      height: 31px;
    }
    button {
      border: none;
      height: 31px;
      font-size: 13px;
      color: #cea26a;
      background-color: transparent;
      padding-right: 9px;
    }
  }
}

/* .col1 {
  font-weight: 500;
  margin-bottom: 5px;
}

.col2 {
  color: #ccc;
  font-weight: 549;
  font-size: 8px;
  margin-bottom: 14px;
} */
</style>
